package com.afkt.project.ui.fragment;


import com.afkt.project.base.app.BaseFragment;
import com.afkt.project.util.LogUtils;
import com.ohos.afkt.ResourceTable;
import com.dev.DevUtils;
import com.dev.base.widget.BaseImageView;
import com.dev.base.widget.BaseTextView;
import com.dev.utils.common.ChineseUtils;
import com.dev.utils.common.RandomUtils;
import ohos.agp.components.Component;
import ohos.global.resource.NotExistException;
import ohos.media.image.ImageSource;
import ohos.media.image.PixelMap;

import java.io.IOException;
import java.io.InputStream;

public class PagerFragment extends BaseFragment {

    private static final String TAG = "PagerFragment";

    private int position = 0;

    //控件
    private BaseTextView vidFpPrefaceTv;
    private BaseTextView vidFpTitleTv;
    private BaseTextView vidFpContentTv;
    private BaseImageView vidFpIgview;


    public PagerFragment(int position) {
        this.position = position;
    }

    @Override
    public int baseContentId() {
        return ResourceTable.Layout_pageslider_pager;
    }

    @Override
    public void onViewCreated(Component rootView) {
        vidFpPrefaceTv = (BaseTextView) rootView.findComponentById(ResourceTable.Id_vid_fp_preface_tv);
        vidFpTitleTv = (BaseTextView) rootView.findComponentById(ResourceTable.Id_vid_fp_title_tv);
        vidFpContentTv = (BaseTextView) rootView.findComponentById(ResourceTable.Id_vid_fp_content_tv);
        vidFpIgview = (BaseImageView) rootView.findComponentById(ResourceTable.Id_vid_fp_igview);


        vidFpPrefaceTv.setText(ChineseUtils.randomWord(RandomUtils.getRandom(30, 100)));
        vidFpTitleTv.setText(position + "." + ChineseUtils.randomWord(5));
        vidFpContentTv.setText(ChineseUtils.randomWord(400));
        vidFpIgview.setPixelMap(getPixelMap(position));

    }

    private PixelMap getPixelMap(int position) {
        int resourceId = -1;

        switch (position) {
            case 1:
                resourceId = ResourceTable.Media_wallpaper_1;
                break;
            case 2:
                resourceId = ResourceTable.Media_wallpaper_2;
                break;
            case 3:
                resourceId = ResourceTable.Media_wallpaper_3;
                break;
            case 4:
                resourceId = ResourceTable.Media_wallpaper_4;
                break;
        }
        InputStream inputStream = null;
        try {
            // 创建图像数据源ImageSource对象
            inputStream = DevUtils.getContext().getResourceManager().getResource(resourceId);
            ImageSource.SourceOptions srcOpts = new ImageSource.SourceOptions();
            srcOpts.formatHint = "image/jpg";
            ImageSource imageSource = ImageSource.create(inputStream, srcOpts);
            // 设置图片参数
            ImageSource.DecodingOptions decodingOptions = new ImageSource.DecodingOptions();
            return imageSource.createPixelmap(decodingOptions);
        } catch (IOException e) {
            LogUtils.error(TAG, "IOException");
        } catch (NotExistException e) {
            LogUtils.error(TAG, "NotExistException");
        } finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    LogUtils.error(TAG, "inputStream IOException");
                }
            }
        }
        return null;
    }
}
